package com.fzf.org.controller;

import com.fzf.org.dto.req.admin.AdminLoginReqDTO;
import com.fzf.org.dto.req.admin.AdminUpdateReqDTO;
import com.fzf.org.dto.req.admin.TakedownReqDTO;
import com.fzf.org.dto.req.admin.UserBanReqDTO;
import com.fzf.org.dto.req.user.UserRegisterReqDTO;
import com.fzf.org.dto.resp.admin.*;
import com.fzf.org.dto.resp.blog.BlogDetailRespDTO;
import com.fzf.org.dto.resp.blog.BlogPageRespDTO;
import com.fzf.org.page.PageRequest;
import com.fzf.org.page.PageResponse;
import com.fzf.org.result.Result;
import com.fzf.org.result.Results;
import com.fzf.org.service.AdminService;
import jakarta.validation.Valid;
import jakarta.validation.constraints.Size;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


@RequestMapping("/blog/admin")
@RestController
@RequiredArgsConstructor
public class AdminController {

    private final AdminService adminService;

    @GetMapping("/check/health")
    public Result<Boolean> checkAccountIfHealth() {
        return Results.success(true);
    }

    @GetMapping("/check/normal")
    public Result<Boolean> checkAccountIfNormal(@Valid @Size(min = 6, max = 20) String account) {
        return Results.success(adminService.checkAccountIfNormal(account));
    }

    @PostMapping("/login")
    public Result<String> login(@Valid @RequestBody AdminLoginReqDTO adminLoginReqDTO) {
        return Results.success(adminService.login(adminLoginReqDTO));
    }

    @PostMapping("/upload")  // ok
    public Result<Void> uploadPicture(@RequestParam("pic") MultipartFile pic) {
        adminService.uploadPicture(pic);
        return Results.success();
    }

    @GetMapping
    public Result<AdminSelfRespDTO> profile() {
        return Results.success(adminService.profile());
    }

    @DeleteMapping("/logout")
    public Result<Void> logout() {
        adminService.logout();
        return Results.success();
    }

    @PutMapping
    public Result<Void> updateAdminInfo(@Valid @RequestBody AdminUpdateReqDTO adminUpdateReqDTO) {
        adminService.updateAdminInfo(adminUpdateReqDTO);
        return Results.success();
    }

    /**
     * 分页获取用户信息
     *
     * @param pageRequest 分页请求参数，包含当前页码和每页大小
     * @param status      在线状态过滤条件，为null时不过滤
     * @param searchQuery 搜索关键词，用于模糊匹配账号和电话号码，为空时不过滤
     * @return 分页响应结果，包含转换后的用户信息列表
     */
    @GetMapping("/user/all")
    public Result<PageResponse<UserPageRespDTO>> getUserInfoByPage(PageRequest pageRequest,
                                                                   @RequestParam(required = false) Boolean status,
                                                                   @RequestParam(required = false) String searchQuery) {
        return Results.success(adminService.getUserInfoByPage(pageRequest, status, searchQuery));
    }

    @GetMapping("/user/detail")
    public Result<UserDetailRespDTO> getUserDetail(@RequestParam("account") String account) {
        return Results.success(adminService.getUserDetail(account));
    }

    @PostMapping("/user/add")
    public Result<Boolean> applyAccount(@Valid @RequestBody UserRegisterReqDTO userRegisterReqDTO) {
        return Results.success(adminService.applyAccount(userRegisterReqDTO));
    }

    @DeleteMapping("/user/kick")
    public Result<Void> kickOutUser(@RequestParam("account") String account) {
        adminService.kickOutUser(account);
        return Results.success();
    }

    @DeleteMapping("/user/ban")
    public Result<Void> banUser(UserBanReqDTO userBanReqDTO) {
        adminService.banUser(userBanReqDTO);
        return Results.success();
    }

    @GetMapping("/user/unban")
    public Result<Void> unBanUser(@RequestParam("account") String account) {
        adminService.unBanUser(account);
        return Results.success();
    }

    @GetMapping("/user/abnormal")
    public Result<PageResponse<UserAbnormalPageRespDTO>> getAbnormalUserByPage(PageRequest pageRequest) {
        return Results.success(adminService.getAbnormalUserByPage(pageRequest));
    }

    /**
     * 分页获取管理员信息
     *
     * @param pageRequest
     * @param name
     * @param status
     * @param searchQuery
     * @apiNote 当account不为空时，查询该账号下的所有博客 <br>
     * 当status不为空时，查询该状态下的所有博客<br>
     * 当searchQuery不为空时，查询标题或内容包含该关键词的博客<br>
     */
    @GetMapping("/blogs")
    public Result<PageResponse<BlogPageRespDTO>> getBlogsByPage(PageRequest pageRequest,
                                                                @RequestParam(required = false) String name,
                                                                @RequestParam(required = false) String status,
                                                                @RequestParam(required = false) String searchQuery) {
        return Results.success(adminService.getBlogsByPage(pageRequest, name, status, searchQuery));
    }

    @GetMapping("/blogs/{blogId}")
    public Result<BlogDetailRespDTO> getBlogDetail(@PathVariable Long blogId) {
        return Results.success(adminService.getBlogDetail(blogId));
    }

    @PostMapping("/blogs/takedown")
    public Result<Boolean> takedownBlog(@RequestBody TakedownReqDTO dto) {
        return Results.success(adminService.takedownBlog(dto));
    }

    @GetMapping("/audits")
    public Result<PageResponse<AuditRespDTO>> getAuditByPage(PageRequest pageRequest) {
        return Results.success(adminService.getAuditByPage(pageRequest));
    }
}
